package com.wys.mall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程编排
 *
 * @Package com.wys.mall.search.thread
 * @Author charlin
 * @Version: v1.0
 * @Date 2021-07-0314:35
 * @Copyright: 2021 www.wyscha.com Inc. All rights reserved.
 */
public class CompletableFutureTest {

    public static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {

//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            Integer i = 10 / 2;
//            System.out.println("结果：" + i);
//        }, executorService);
//        System.out.println(future.get());

//        CompletableFuture<Integer> supplyFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            Integer i = 10 / 0;
//            System.out.println("计算结果：" + i);
//            return i;
//        }, executorService).whenComplete((res,err) ->{
//            System.out.println("返回结果是：" + res);
//            System.out.println("异常原因：" + err);
//        }).exceptionally(throwable -> { //出现异常处理，默认返回0
//            return 0;
//        });

//        System.out.println(supplyFuture.get());

        // 异常处理
//        CompletableFuture<Integer> supplyFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            Integer i = 10 / 0;
//            System.out.println("计算结果：" + i);
//            return i;
//        }, executorService).handle((res, err) ->{
//            if(res != null){
//                return res;
//            }
//            if(err != null){
//                return 0;
//            }
//            return null;
//        });
//
//        System.out.println(supplyFuture.get());

        //thenRunAsync不能获得上一步的执行结果
//        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            Integer i = 10 / 0;
//            System.out.println("计算结果：" + i);
//            return i;
//        }, executorService).thenRunAsync(() -> {
//            System.out.println("任务2启动了....");
//        }, executorService);

        //thenAcceptAsync能获得上一步的执行结果
//        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            Integer i = 10 / 0;
//            System.out.println("计算结果：" + i);
//            return i;
//        }, executorService).thenAcceptAsync(t -> {
//            System.out.println("任务2启动了....获取了任务1的结果" + t);
//        }, executorService);

        //thenRunAsync不能获得上一步的执行结果
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            Integer i = 10 / 0;
//            System.out.println("计算结果：" + i);
//            return i;
//        }, executorService).thenApplyAsync(res -> {
//            System.out.println("任务2启动了....获取了任务1的结果" + res);
//            return "结果：" + res;
//        }, executorService);

        //合并------------------------------------------------------
//        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程1：" + Thread.currentThread().getId());
//            Integer i = 10 / 3;
//            return i;
//        }, executorService);
//
//        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
//            String hello = "hello tom";
//            try {
//                Thread.sleep(1000);
//                System.out.println("当前线程2：" + Thread.currentThread().getId());
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            return hello;
//        }, executorService);
//
//        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程3：" + Thread.currentThread().getId());
//            Integer i = 10 / 3;
//            return i + "";
//        }, executorService);

        // 两个任务都完成才执行第三个任务------------------------------------------------------------
//        future1.runAfterBothAsync(future2, () -> {
//            System.out.println("当前线程3：" + Thread.currentThread().getId());
//        }, executorService);
//
//        //获得线程1和线程2
//        future1.thenAcceptBothAsync(future2, (f1, f2) -> {
//            System.out.println("当前线程3：" + Thread.currentThread().getId()+ " 之前的结果：" + f1  +  ":" + f2) ;
//        }, executorService);
//
//        CompletableFuture<String> combineAsync = future1.thenCombineAsync(future2, (f1, f2) -> {
//            System.out.println("当前线程3：" + Thread.currentThread().getId() + " 之前的结果：" + f1 + ":" + f2);
//            return f1 + ":" + f2;
//        }, executorService);
//        System.out.println("f3==" + combineAsync.get());


        //合并-----任一个任务都完成才执行第三个任务------------------------------------------------------------

        //不能接收上次结果
//        future1.runAfterEitherAsync(future2, () -> {
//            System.out.println("当前线程3：" + Thread.currentThread().getId());
//        }, executorService);

        //能接收的结果
//        future3.acceptEitherAsync(future2, res -> {
//            System.out.println("当前线程4：" + Thread.currentThread().getId() + "  结果是：" + res);
//        }, executorService);

        //能接收的结果，并返回新的结果
//        future3.applyToEitherAsync(future2, res -> {
//            System.out.println("当前线程4：" + Thread.currentThread().getId() + "  结果是：" + res);
//            return res;
//        }, executorService);

        //合并-----所有任务全部完成------------------------------------------------------------
        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询图片");
            return "img";
        },executorService);
        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("查询属性");
            return "attr";
        },executorService);
        CompletableFuture<String> futureBrand = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询品牌");
            return "brand";
        },executorService);
        //--全部完成-----------------
//        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureBrand);
//        System.out.println(allOf.get());

        //--任何一个完成-----------------
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureBrand);
        System.out.println(anyOf.get());

        System.out.println("end----------------------------");

    }
}
